package cn.yuanqiao.archive.storeRoom.service.impl;

import cn.yuanqiao.archive.appManager.domain.TenantSysDictData;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictTypeService;
import cn.yuanqiao.archive.storeRoom.domain.*;
import cn.yuanqiao.archive.storeRoom.mapper.GridMapper;
import cn.yuanqiao.archive.storeRoom.mapper.ShelfMapper;
import cn.yuanqiao.archive.storeRoom.mapper.StoreRoleMapper;
import cn.yuanqiao.archive.storeRoom.mapper.StoreroomMapper;
import cn.yuanqiao.archive.storeRoom.service.IStoreRoleService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 库房权限Service业务层处理
 *
 * @author xy
 * @date 2023-06-20
 */
@Service
public class StoreRoleServiceImpl implements IStoreRoleService
{
    @Autowired
    private StoreRoleMapper storeRoleMapper;
    @Autowired
    private ShelfMapper shelfMapper;
    @Autowired
    private GridMapper gridMapper;
    @Autowired
    private StoreroomMapper storeroomMapper;

    @Autowired
    private ITenantSysDictTypeService sysDictTypeService;

    /**
     * 查询库房权限
     *
     * @param storeId 库房权限主键
     * @return 库房权限
     */
    @Override
    public StoreRole selectStoreRoleByStoreId(Long storeId)
    {
        return storeRoleMapper.selectStoreRoleByStoreId(storeId);
    }

    /**
     * 查询库房权限列表
     *
     * @param storeRole 库房权限
     * @return 库房权限
     */
    @Override
    public List<StoreRole> selectStoreRoleList(StoreRole storeRole)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            throw new ServiceException("TenantCode is null");
          //  return storeRoleMapper.selectStoreRoleList(storeRole,storeRole.getUserIds(),"");

        }else{
            return storeRoleMapper.selectStoreRoleList(storeRole,storeRole.getUserIds(),tenantCode.toUpperCase());

        }
    }

    /**
     * 新增库房权限
     *
     * @param storeRole 库房权限
     * @return 结果
     */
    @Override
    public int insertStoreRole(StoreRole storeRole)
    {
        return storeRoleMapper.insertStoreRole(storeRole);
    }

    /**
     * 修改库房权限
     *
     * @param storeRole 库房权限
     * @return 结果
     */
    @Override
    public int updateStoreRole(StoreRole storeRole)
    {
        return storeRoleMapper.updateStoreRole(storeRole);
    }

    /**
     * 批量删除库房权限
     *
     * @param storeIds 需要删除的库房权限主键
     * @return 结果
     */
    @Override
    public int deleteStoreRoleByStoreIds(Long[] storeIds)
    {
        return storeRoleMapper.deleteStoreRoleByStoreIds(storeIds,SecurityUtils.getTenantCode().toUpperCase());
    }

    @Override
    public List<UserRole> selectStoreUserList() {
        String tenantCode = SecurityUtils.getTenantCode();
        List<UserRole> list = storeRoleMapper.selectStoreUserList(tenantCode);
        return list;
    }

    @Override
    public List getTreeList() {
        String tenantCode = SecurityUtils.getTenantCode().toUpperCase();

        if (StringUtils.isEmpty(tenantCode)) {
            throw new ServiceException("租户code不能为空！");

        }
        List<Storeroom> storeroom = storeroomMapper.selectStoreroomListByRole(new Storeroom(), tenantCode.toUpperCase(),SecurityUtils.getUserId());
        List<Shelf> shelfList = shelfMapper.selectShelfList(new Shelf(), tenantCode.toUpperCase());
        TreeStore rootTree = new TreeStore();
        rootTree.setId("0");
        rootTree.setLabel("库房");
        rootTree.setNodeType("root");
        List<TreeStore> chilTrees = new ArrayList<>();
        for (Storeroom mf : storeroom) {
            TreeStore chilTree = new TreeStore();
            chilTree.setId(mf.getStoreId().toString());
            chilTree.setLabel(mf.getStoreName());
            chilTree.setTenantCode(mf.getRegion());
            chilTree.setNodeType("store");
            List<TreeStore> treeSelects = new ArrayList<>();
            Integer isFenqu = mf.getIsFenqu();
            List<String> list = new ArrayList();
            if (isFenqu == 1) {
                String region = mf.getRegion();
                if (region.indexOf(";") > -1) {
                    list = Arrays.asList(region.split(";"));
                } else {
                    list.add(region);
                }
                for (String re : list) {
                    TreeStore chilTree2 = new TreeStore();
                    chilTree2.setId(mf.getStoreId()+"_"+re);
                    //获取值
                    String dictValue = null;
                    List<TenantSysDictData> dictDataList = sysDictTypeService.selectDictDataByType("REGION");  //BGQX
                    for (TenantSysDictData dict : dictDataList) {
                        if (dict.getDictLabel().equals(re) || dict.getDictValue().equals(re)) {  // 10年   D10
                            dictValue = dict.getDictLabel();
                            break;
                        }
                    }
                        chilTree2.setLabel(dictValue);
                        chilTree2.setTenantCode(re);
                        chilTree2.setNodeType("region");
                        treeSelects.add(chilTree2);
                        List<TreeStore> treeSelects2 = new ArrayList<>();
                        for (Shelf shelf : shelfList) {
                            if ((shelf.getStoreId().equals(mf.getStoreId()) ) && (shelf.getKfqy().equals(re))) {
                                TreeStore chilTree3 = new TreeStore();
                                chilTree3.setId(shelf.getShelfId().toString());
                                chilTree3.setLabel(shelf.getShelfName());
                                chilTree3.setTenantCode(tenantCode);
                                chilTree3.setNodeType("shelf");
                                treeSelects2.add(chilTree3);
                            }
                        }
                        if (treeSelects2.size() > 0) {
                            chilTree2.setChildren(treeSelects2);
                        }


                }
            /*    if (treeSelects.size() > 0) {
                    chilTree.setChildren(treeSelects);
                }*/
            } else {
                for (Shelf shelf : shelfList) {
                    if (mf.getStoreId() .equals(shelf.getStoreId()) ) {
                        TreeStore chilTree2 = new TreeStore();
                        chilTree2.setId(shelf.getShelfId().toString());
                        chilTree2.setLabel(shelf.getShelfName());
                        chilTree2.setTenantCode("");
                        chilTree2.setNodeType("shelf");
                        treeSelects.add(chilTree2);
                    }
                }

            }
            if (treeSelects.size() != 0) chilTree.setChildren(treeSelects);
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeStore> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }
}
